package com.example.sync.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.sync.po.SysASyncJob;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * @author chentao
 * @version 1.0
 * @description: TODO
 * @date 2025/3/22 下午1:40
 */
@Mapper
public interface SysASyncJobMapper extends BaseMapper<SysASyncJob> {

    @Select("select * from sys_async_job where server_id =#{serverId} and status = 0 and ((retry_count = 0) or (retry_count < #{maxRetryCount} and retry_time < now())) order by id asc LIMIT #{maxSize}")
    public List<SysASyncJob> getSysASyncJob(@Param("serverId") Long serverId, @Param("maxSize") int maxSize, @Param("maxRetryCount") int maxRetryCount);

    @Update("update sys_async_job set server_id = #{targetServerId} where server_id =#{sourceServerId} and `status` = 0 and id % #{total} = #{index} ")
    public void updateASyncJobServerBatch(@Param("sourceServerId") Long sourceServerId, @Param("targetServerId") Long targetServerId, @Param("index") int index, @Param("total") int total);

    @Update(value = "update sys_async_job set server_id = #{targetServerKey} where server_id is null order by id asc LIMIT #{countSize}")
    public void updateASyncServer(@Param("targetServerKey") Long targetServerKey, @Param("countSize") int countSize);
}
